package com.yunfeisoft.business.dao.impl.postgres;

import com.applet.base.ServiceDaoImpl;
import com.applet.sql.builder.SelectBuilder;
import com.applet.sql.builder.WhereBuilder;
import com.applet.utils.Page;
import com.yunfeisoft.business.dao.inter.PmsProductAttributeDao;
import com.yunfeisoft.business.model.PmsProductAttribute;
import com.yunfeisoft.business.model.PmsProductAttributeCategory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * ClassName: PmsProductAttributeDaoImpl
 * Description: 商品属性表Dao实现
 * Author: Jackie liu
 * Date: 2020-06-02
 */
@Repository
public class PmsProductAttributeDaoImpl extends ServiceDaoImpl<PmsProductAttribute, String> implements PmsProductAttributeDao {

    @Override
    public Page<PmsProductAttribute> queryPage(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        wb.setOrderByWithAsc("pa.sort");
        if (params != null) {
            initPageParam(wb, params);
            wb.andEquals("pa.productAttributeCategoryId", params.get("categoryId"));
            wb.andEquals("pa.type", params.get("type"));
            wb.andFullLike("pa.name", params.get("name"));
        }

        SelectBuilder builder = getSelectBuilder("pa");
        builder.column("pac.name as productAttributeCategoryName")
                .join(PmsProductAttributeCategory.class).alias("pac").on("pa.productAttributeCategoryId = pac.id").build();

        return queryPage(builder.getSql(), wb);
    }

    @Override
    public int queryNum(String productAttributeCategoryId, int type) {
        if (StringUtils.isBlank(productAttributeCategoryId)) {
            return 0;
        }

        WhereBuilder wb = new WhereBuilder();
        wb.andEquals("productAttributeCategoryId", productAttributeCategoryId);
        wb.andEquals("type", type);

        return count(wb);
    }

    @Override
    public List<PmsProductAttribute> queryList(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        wb.setOrderByWithAsc("sort");
        if (params != null) {
            wb.andEquals("productAttributeCategoryId", params.get("categoryId"));
            wb.andEquals("type", params.get("type"));
            wb.andFullLike("name", params.get("name"));
        }

        return query(wb);
    }
}